package com.zbs.framework.security.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.Locale;

@Component
@ConfigurationProperties(prefix = "framework")
public class FrameworkConstants {
    //token名称
    public static String TOKEN_NAME = "X-Token";
    public static String SIGN_NAME = "sign";
    //token秘钥
    public static String TOKEN_SIGN_KEY;
    //token 有效时间
    public static long TOKEN_EXPIRATION = 60*60*24*3;
    // framework chat websocket 是否开启token校验
    public static boolean CHAT_IS_OPEN_TOKEN = false;
    //是否开启服务端权限校验
    public static boolean DO_OPEN_PERMISSIONS = true;
    // 可选值：ALL/annotations 说明： all/annotations 默认值：annotations 当 do-open-permissions 为true 时 permissions-model配置生效
    // all:拦截所有API
    // annotations: 只拦截@PreAuthorize的注解的方法
    public static String PERMISSIONS_MODEL = "annotations";
    //是否开启单点登录
    public static boolean IS_SSO = true;
    //是否开启多端登录
    public static boolean IS_MULTI_TERMINAL = false;
    //退出登录地址
    public static String LOGOUT_URL;
    //放行地址 服务端带有权限的接口不可放行
    public static String[] IGNORING_URL;
    //静态资源放行
    public static String[] STATIC_IGNORING_URL;
    //默认管理员id
    public static Long[] DEFAULT_ADMIN;

    public void setDoOpenPermissions(boolean doOpenPermissions) {
        FrameworkConstants.DO_OPEN_PERMISSIONS = doOpenPermissions;
    }

    public void setSignName(String signName) {
        FrameworkConstants.SIGN_NAME = signName;
    }

    public void setPermissionsModel(String permissionsModel) {
        if (StringUtils.hasLength(permissionsModel)) {
            permissionsModel = permissionsModel.toLowerCase(Locale.ROOT);
        }
        FrameworkConstants.PERMISSIONS_MODEL = permissionsModel;
    }

    public void setChatIsOpenToken(boolean chatIsOpenToken) {
        FrameworkConstants.CHAT_IS_OPEN_TOKEN = chatIsOpenToken;
    }

    public void setStaticIgnoringUrl(String[] staticIgnoringUrl) {
        FrameworkConstants.STATIC_IGNORING_URL = staticIgnoringUrl;
    }

    public void setIsSso(boolean isSSO) {
        FrameworkConstants.IS_SSO = isSSO;
    }

    public void setIsMultiTerminal(boolean isMultiTerminal) {
        FrameworkConstants.IS_MULTI_TERMINAL = isMultiTerminal;
    }

    public void setTokenName(String tokenName) {
        FrameworkConstants.TOKEN_NAME = tokenName;
    }

    public void setTokenExpiration(long tokenExpiration) {
        FrameworkConstants.TOKEN_EXPIRATION = tokenExpiration*1000;
    }

    public void setTokenSignKey(String tokenSignKey) {
        FrameworkConstants.TOKEN_SIGN_KEY = tokenSignKey;
    }


    public void setLogoutUrl(String logoutUrl) {
        FrameworkConstants.LOGOUT_URL = logoutUrl;
    }

    public void setIgnoringUrl(String[] ignoringUrl) {
        FrameworkConstants.IGNORING_URL = ignoringUrl;
    }

    public void setDefaultAdmin(Long[] defaultAdmin) {
        FrameworkConstants.DEFAULT_ADMIN = defaultAdmin;
    }
}
